package models

import (
	"GoTime/response"
	"log"
	"time"

	"xorm.io/builder"
)

type LearnRecord struct {
	Id                int       `xorm:"not null pk autoincr comment('自增主键') INT(11)"`
	SchoolId          int       `xorm:"not null comment('驾校id外键关联dts_school表中的id') index INT(11)"`
	StudentId         int       `xorm:"not null comment('学员id外键关联dts_student表中的id') index INT(11)"`
	CoachId           int       `xorm:"not null comment('教练员id外键关联dts_coach表中的id') index INT(11)"`
	CarId             int       `xorm:"not null comment('教练车id外键关联dts_car表中的id') index INT(11)"`
	TerminalId        int       `xorm:"not null comment('计时终端设备id外键关联dts_terminal中的id') index INT(11)"`
	Type              string    `xorm:"default '0' comment('该段学时类型标识，1：实车教学,2:课堂教学,3:模拟器教学,4:远程教学') ENUM('0','1','2','3','4','5')"`
	Subject           string    `xorm:"not null default '1' comment('科目一二三四') ENUM('1','2','3','4')"`
	StartTime         time.Time `xorm:"not null comment('学时开始时间') index DATETIME"`
	EndTime           time.Time `xorm:"not null comment('学时结束时间') index DATETIME"`
	LastingMinute     int       `xorm:"comment('此次学车持续时间单位分钟') SMALLINT(5)"`
	ApprovalStatus    string    `xorm:"default '0' comment('本次学时提交运管审核状态，0:待审核，1:合格，2:不合格') ENUM('0','1','2')"`
	CheckStatus       string    `xorm:"default '0' comment('四川审核状态，0:待审核，1:合格，2:不合格') ENUM('0','1','2')"`
	RecordStatus      string    `xorm:"not null default '0' comment('备案状态 0:未备案 1:已备案') ENUM('0','1')"`
	Mileage           float32   `xorm:"comment('里程') FLOAT(12,3)"`
	MaxVelocity       float32   `xorm:"comment('最大速度 km/h') FLOAT(12,3)"`
	Reason            string    `xorm:"comment('未通过审核的原因  ') TEXT"`
	LearnrecordNumber string    `xorm:"comment('课堂id') index CHAR(26)"`
	Coachloginid      int       `xorm:"INT(11)"`
	Course            string    `xorm:"comment('课程编码（10位属性码）') VARCHAR(10)"`
	Sn                string    `xorm:"comment('学时编号') VARCHAR(21)"`
	Lastsn            int       `xorm:"default 0 INT(11)"`
	Mongoid           string    `xorm:"default '' index VARCHAR(24)"`
	Ispost            string    `xorm:"default '0' comment('是否已上报公安系统  0 未上报  1 已上报') ENUM('0','1')"`
	Isreport          string    `xorm:"default '0' comment('0未进行上报，1已经上报') ENUM('0','1')"`
	Iscongye          string    `xorm:"default '0' comment('0否，1是') ENUM('0','1')"`
	Iskaohe           string    `xorm:"default '0' comment('0否，1是') ENUM('0','1')"`
	ReportNum         string    `xorm:"default '' comment('上报备案编号') VARCHAR(255)"`
	Ischeck           int       `xorm:"default 0 comment('0未过滤1已过滤') index SMALLINT(6)"`
	RecordTime        time.Time `xorm:"not null default 'CURRENT_TIMESTAMP' comment('备案时间') DATETIME"`
	Xsnumber          string    `xorm:"-"`
	AverageSpeed      float32   `xorm:"-"`
	Isneed            string    `xorm:"-"`
}

func (l *LearnRecord) GetSchoolRecordList(filter builder.Cond, pageSize int, offset int) (count int64, list []*response.RecordListResponse, err error) {
	err = engine.Table("dts_learn_record").Alias("ld").
		Select("ld.id,ld.school_id,ld.subject,ld.start_time,ld.end_time,ld.lasting_minute,ld.approval_status,ld.record_status,ld.mileage,car.plate_number,uc.name as coach_name,user.name,user.phone,user.id_number,st.training_car_type").
		Join("INNER", "dts_student st", "ld.student_id = st.id").
		Join("INNER", "dts_user user", "st.user_id = user.id").
		Join("LEFT", "dts_car car", "ld.car_id = car.id").
		Join("LEFT", "dts_coach coach", "ld.coach_id = coach.id").
		Join("LEFT", "dts_user uc", "coach.user_id = uc.id").
		And(filter).Limit(pageSize, offset).Desc("ld.id").Find(&list)
	log.Println(err)
	count, err = engine.Table("dts_learn_record").Alias("ld").
		Select("COUNT(ld.id)").
		Join("INNER", "dts_student st", "ld.student_id = st.id").
		Join("INNER", "dts_user user", "st.user_id = user.id").
		Join("LEFT", "dts_car car", "ld.car_id = car.id").
		Join("LEFT", "dts_coach coach", "ld.coach_id = coach.id").
		Join("LEFT", "dts_user uc", "coach.user_id = uc.id").
		And(filter).Count()
	return count, list, err
}

func (l *LearnRecord) GetStudentRecordList(filter builder.Cond) (list []*response.RecordListResponse, err error) {
	err = engine.Table("dts_learn_record").Alias("ld").
		Select("ld.id,ld.school_id,ld.subject,ld.type,ld.start_time,ld.end_time,ld.lasting_minute,ld.approval_status,ld.record_status,ld.mileage,car.plate_number,uc.name as coach_name").
		//Join("INNER", "dts_student st", "ld.student_id = st.id").
		//Join("INNER", "dts_user user", "st.user_id = user.id").
		Join("LEFT", "dts_car car", "ld.car_id = car.id").
		Join("LEFT", "dts_coach coach", "ld.coach_id = coach.id").
		Join("LEFT", "dts_user uc", "coach.user_id = uc.id").
		And(filter).Desc("ld.id").Find(&list)

	return list, err
}
